package cn.cloudreal.cbms.system.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.system.dao.OrganizaDao;
import cn.cloudreal.cbms.system.dao.PurviewDao;
import cn.cloudreal.cbms.system.dao.RoleDao;
import cn.cloudreal.cbms.system.dao.UserInfoDao;
import cn.cloudreal.cbms.system.service.UserInfoService;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.Organiza;
import cn.cloudreal.cbms.vo.Purview;
import cn.cloudreal.cbms.vo.Role;
import cn.cloudreal.cbms.vo.UserInfo;

/**
 * <p>
 * Title: CBMS
 * </p>
 * <p>
 * Description:用户信息查询数据服务层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author gaoxuejiao
 * @date 2012-07-17
 */
public class UserInfoServiceImpl implements UserInfoService
{
    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(UserInfoServiceImpl.class);

    /** 查询数据dao */
    private UserInfoDao userInfoDao;

    private RoleDao roleDao;
    private PurviewDao purviewDao;
    private OrganizaDao organizaDao;

    /**
     * 组织编号查用户
     * 
     * @param orgCode
     * @return List<UserInfo>
     */
    public List<UserInfo> getUserInfoByOrgCode(String orgCode)
    {
        return userInfoDao.getUserInfoByOrgCode(orgCode);
    }

    /**
     * 用户登录对象
     * 
     * @param userInfo
     * @return
     */
    public UserInfo loginUser(UserInfo userInfo)
    {

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoServiceImpl UserInfo() method");
        }

        // 声明对象
        UserInfo reUserInfo = null;

        // 根据用户对象查询当前用户对象
        reUserInfo = userInfoDao.getUserInfo(userInfo);

        // 如果获取对象不为空
        if (null != reUserInfo)
        {
            // 地市中文
            if (null != reUserInfo.getUsercity())
            {
                Dictionary dic = DatadicContext.getInstance()
                        .getDatadicObject(reUserInfo.getUsercity(),
                                Constants.CITY_AREA_CODE);
                if (null != dic)
                {
                    // 数据字典中获取
                    reUserInfo.setCityName(dic.getDatavalue());
                }

            }
            // 根据编号获取角色对象
            if (null != reUserInfo.getRoleCode())
            {
                // 存储角色对象
                Role role = roleDao.getAllRoleByCode(reUserInfo.getRoleCode());
                if (null != role)
                {
                    reUserInfo.setUserRole(role);
                    // 根据角色获取权限 集合
                    List<Purview> purList = purviewDao.getPurviewsByRole(role.getRolecode());
                    if (null != purList && purList.size() > 0)
                    {
                        reUserInfo.setPurviewList(purList);
                    }
                }
            }
            // 获取组织架构集合
            if (null != reUserInfo.getBlongOrgCode())
            {
                Organiza org = organizaDao.getOrganizaByCode(reUserInfo.getBlongOrgCode());
                if (null != org)
                {
                    reUserInfo.setOrganiza(org);
                }
            }
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit UserInfoServiceImpl UserInfo() method");
        }

        return reUserInfo;
    }

    /**
     * 查询数据列表
     * 
     * @return
     */
    public List<UserInfo> getUserInfos(UserInfo userInfo, PageBean page)
    {
        return userInfoDao.getUserInfos(userInfo, page);
    }

    /**
     * 查询单个数据
     * 
     * @return
     */
    public UserInfo getUserInfo(Integer id)
    {
        return userInfoDao.getUserInfo(id);
    }

    /**
     * 更新
     * 
     * @return
     */
    public void updateUserInfo(UserInfo userInfo)
    {
        userInfoDao.updateUserInfo(userInfo);

    }

    /**
     * 删除
     * 
     * @return
     */
    public void delUserInfo(UserInfo userInfo)
    {
        userInfoDao.delUserInfo(userInfo);

    }

    /**
     * 查询用户账号是否已经存在
     * 
     * @param userInfo
     * @return boolean
     */
    public boolean isExistUserCode(UserInfo userInfo)
    {

        return userInfoDao.isExistUserCode(userInfo);
    }

    /**
     * 新增
     * 
     * @return
     */
    public void addUserInfo(UserInfo userInfo)
    {
        userInfoDao.addUserInfo(userInfo);

    }

    public RoleDao getRoleDao()
    {
        return roleDao;
    }

    public void setRoleDao(RoleDao roleDao)
    {
        this.roleDao = roleDao;
    }

    public PurviewDao getPurviewDao()
    {
        return purviewDao;
    }

    public void setPurviewDao(PurviewDao purviewDao)
    {
        this.purviewDao = purviewDao;
    }

    public OrganizaDao getOrganizaDao()
    {
        return organizaDao;
    }

    public void setOrganizaDao(OrganizaDao organizaDao)
    {
        this.organizaDao = organizaDao;
    }

    public UserInfoDao getUserInfoDao()
    {
        return userInfoDao;
    }

    public void setUserInfoDao(UserInfoDao userInfoDao)
    {
        this.userInfoDao = userInfoDao;
    }

}
